#include "login.h"
#include "ui_login.h"
#include "database.h"  //数据库
#include "userpage.h"




QString Now_Username = "";//当前用户
QString Now_Password = "";//当前密码

login::login(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::login)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint); //去除标题框

    //窗口不规则化
    "image: url(:/icon/backgroud3.png);";
    QPixmap pixmap;
    pixmap.load(":/icon/9.png");
    setAttribute(Qt::WA_TranslucentBackground);
    resize(pixmap.size());
    setMask(pixmap.mask());


}


login::~login()
{
    delete ui;
}


//用于窗口不规则图案绘制
void login::paintEvent(QPaintEvent *event)
{
    QPainter p(this);
    p.drawPixmap(0,0,QPixmap(":/1/G:/png/1.png"));
}

//鼠标点击事件
void login::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        dragPosition = event->globalPos() - frameGeometry().topLeft();
    }
//    else if(event->button() == Qt::RightButton){
//        close();
//    }
}

//鼠标移动事件
void login::mouseMoveEvent(QMouseEvent *event)
{
    if(event->buttons() & Qt::LeftButton){
        move(event->globalPos() - dragPosition);
    }
}


//获取用户名栏信息
QString login::getEditUserName()
{
    Now_Username = ui->lineEdit_username->text();
    return ui->lineEdit_username->text();
}

//获取用户密码栏的信息
QString login::getEditPassword()
{
    Now_Password = ui->lineEdit_password->text();
    return ui->lineEdit_password->text();
}

//清除用户输入栏的信息
void login::clearUserNameEditLine()
{
    ui->lineEdit_username->clear();
}

//清除用户密码栏的信息
void login::clearPasswordEditLine()
{
    ui->lineEdit_password->clear();
}


//注册按钮
void login::on_pushButton_signup_clicked()
{
//    qDebug()<<(ui->lineEdit_username->text().isEmpty());
//    qDebug()<<(ui->lineEdit_password->text().isEmpty());

    if(!(ui->lineEdit_username->text().isEmpty()||ui->lineEdit_password->text().isEmpty()))
    {
        DataBase::UserSignUp(login::getEditUserName(),login::getEditPassword());
//        qDebug() << QObject::tr("注册成功");
        login::clearUserNameEditLine();
        login::clearPasswordEditLine();
    }
    else
    {
//        qDebug()<<"请输入有效信息";
        QMessageBox::information(NULL,"登录","请输入有效信息",QMessageBox::Ok);
    }
}

//登录按钮
void login::on_pushButton_signin_clicked()
{
    if(!(ui->lineEdit_username->text().isEmpty()||ui->lineEdit_password->text().isEmpty()))
    {
        if(!DataBase::querypassword(login::getEditUserName(),login::getEditPassword()))
        {
            //匹配用户密码失败
            QMessageBox::information(NULL,"登录","用户名不存在或密码错误",QMessageBox::Ok);
        }
        else
        {
            //匹配成功
            this->close();
            UserPage *u = new UserPage();
            u->show();

        }

    }
    else
    {
//        qDebug()<<"请输入有效信息";
        QMessageBox::information(NULL,"登录","请输入有效信息",QMessageBox::Ok);
    }

}

//界面退出按钮
void login::on_pushButton_quit_clicked()
{
    close();
}
